package com.mszlu.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.blog.dao.mapper.ArchivesMapper;
import com.mszlu.blog.dao.mapper.ArticleMapper;
import com.mszlu.blog.dao.pojo.Article;
import com.mszlu.blog.dos.Archives;
import com.mszlu.blog.service.ArticleService;
import com.mszlu.blog.service.SysUserService;
import com.mszlu.blog.service.TagService;
import com.mszlu.blog.vo.ArticleVo;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.params.PageParams;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl  implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    ArchivesMapper archivesMapper;

    @Autowired
    TagService tagService;

    @Autowired
    SysUserService userService;

    @Override
    public Result listArticle(PageParams pageParams) {
        /**
         * 分页查询数据库
         */
        Page<Article> page =new Page<>(pageParams.getPage(),pageParams.getPageSize());
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //
        //排序 是够置顶、order by create_date desc, (时间降序)
        queryWrapper.orderByDesc(Article::getWeight,Article::getCreateDate);  //lambda 表达式，传入类型，Article 返回类型weight
        Page<Article> articlePage = articleMapper.selectPage(page, queryWrapper);//查询数据
        List<Article> records = articlePage.getRecords();

        //构造接口返回json数据格式
        List<ArticleVo> articleVoList=copyList(records,true,true);
        return Result.success(articleVoList);

    }

    @Override
    public Result hotArticle(int limit) {
        LambdaQueryWrapper<Article> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Article::getViewCounts);
        queryWrapper.select(Article::getId, Article::getTitle);
        queryWrapper.last("limit " + limit);
        //select id ,title from article order by view_counts desc limit 2;
        //查询id和title字段，前2条，按view_counts 降序
        List<Article> articles = articleMapper.selectList(queryWrapper);
        return Result.success(copyList(articles,false,false));
    }

    @Override
    public Result newArticle(int limit) {
        LambdaQueryWrapper<Article> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Article::getCreateDate);
        queryWrapper.select(Article::getId, Article::getTitle);
        queryWrapper.last("limit " + limit);
        //select id ,title from article order by creat_date desc limit 2;
        //查询id和title字段，前2条，按view_counts 降序
        List<Article> articles = articleMapper.selectList(queryWrapper);
        return Result.success(copyList(articles,false,false));
    }

    @Override
    public Result listArchives() {
        List<Archives> archives =archivesMapper.listArchives();
        return Result.success(archives);
    }

    private List<ArticleVo> copyList(List<Article> records, boolean isTag,boolean isAuthor) {
        List<ArticleVo> articleVoList =new ArrayList();
        for(Article article :records){
            ArticleVo articleVo = copy(article,isTag,isAuthor);
            articleVoList.add(articleVo);
        }
        return  articleVoList;
    }

    /**
     * 复制Article 对象到ArticleVo对象中
     * @param article
     * @return
     */
    private ArticleVo copy(Article article, boolean isTag,boolean isAuthor) {
        ArticleVo articleVo=new ArticleVo();
        BeanUtils.copyProperties(article,articleVo);//复制相同属性到其他类对像
        //不同属性复制  long -->string
        articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
        articleVo.setId(String.valueOf(article.getId()));
        if (isTag){
            Long articleId =article.getId();
            articleVo.setTags(tagService.findTagsByArticleId(articleId));
        }
        if(isAuthor){
            Long authorId = article.getAuthorId();
            articleVo.setAuthor(userService.findUserById(authorId).getNickname());

        }
        return  articleVo;
    }
}
